package project2.entity.my;

import project2.config.Config;
import project2.config.ImageConstants;
import project2.entity.Flyable;
import project2.entity.bullet.Bullet;
import project2.entity.weapon.Bomb;
import project2.factory.BulletFactory;
import project2.resources.Images;
import project2.type.BulletType;
import project2.ui.game.GamePlayingPanel;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 主角飞机
 */
public class MyPlane extends Flyable {
    private boolean isAlive;// 是否存活
    private BulletType bulletType;// 子弹类型
    private List<Bomb> holdBombList;// 持有的炸弹
    private boolean flip;// 是否为小模式
    private final Image planeFlyingImage = Images.MY_PLANE_FLYING_IMG;// 大模式时的形状

    public MyPlane(GamePlayingPanel playingPanel) {
        super.setPlayingPanel(playingPanel);
        super.setWidth(ImageConstants.MY_PLANE_WIDTH);
        super.setHeight(ImageConstants.MY_PLANE_HEIGHT);
        super.setImage(Images.MY_PLANE_IMG);
        this.isAlive = true;
        this.bulletType = BulletType.YELLOW_BULLET;
        this.holdBombList = new LinkedList<>();
        this.flip = true;
        new Thread(() -> {
            while (isAlive) {// 只要飞机存活，就不停发子弹
                try {
                    Thread.sleep(Config.BULLET_FIRE_INTERVAL);
                } catch (InterruptedException ignore) {
                }
                launchBullet();
            }
        }).start();
    }

    // 常用方法
    public void draw(Graphics g) {
        // 每次绘制时大小模型来回切换——看起来就像发射子弹时的后坐力
        if (flip) {
            super.draw(g);
        } else {
            super.drawOtherImage(g, planeFlyingImage);
        }
        flip = !flip;
    }

    public Rectangle getRectangle() {
        int fix = getWidth() / 3;
        return new Rectangle(getPosX() + fix, getPosY(), getWidth() / 3, getHeight());
    }

    /**
     * 飞机随鼠标移动而改变方向
     */
    public void mouseMoved(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        int posX = x - getWidth() / 2;
        setPosX(posX);
        int posY = y - getHeight() / 2;
        setPosY(posY);
    }

    /**
     * 发射子弹
     */
    private void launchBullet() {
        if (isAlive) {
            if (bulletType == BulletType.BLUE_BULLET) {// 创建双排蓝色子弹
                Bullet[] bullets = BulletFactory.createBlueBullet(this);
                Collections.addAll(getPlayingPanel().bullets, bullets);
            } else {// 创建单排橙色子弹
                Bullet bullet = BulletFactory.createYellowBullet(this);
                getPlayingPanel().bullets.add(bullet);// 子弹加入到画板素材中备用
            }
        }
    }

    // get、set方法

    public int getHoldBombCount() {
        return this.holdBombList.size();
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }

    public List<Bomb> getHoldBombList() {
        return holdBombList;
    }

    public void setHoldBombList(List<Bomb> holdBombList) {
        this.holdBombList = holdBombList;
    }

    public BulletType getBulletType() {
        return bulletType;
    }

    public void setBulletType(BulletType bulletType) {
        this.bulletType = bulletType;
    }
}
